﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Script.Serialization;
using Entity;
using Business;
using System.Web.UI;

namespace NetFlow
{
    public partial class InOutAnalysis : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string filePath = Session["FilePath"].ToString(); // 替换为实际文件路径
                AnalysisBusiness analysisBusiness = new AnalysisBusiness();
                List<NetFlowEntity> netFlowList = analysisBusiness.ParseNetFlowData(filePath);

                // 对入方向和出方向的数据进行统计
                var inOutData = CalculateInOutData(netFlowList);

                // 序列化数据
                string jsonData = new JavaScriptSerializer().Serialize(inOutData);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ChartData", $"var inOutData = {jsonData};", true);

                // 序列化原始数据
                string netFlowDataJson = new JavaScriptSerializer().Serialize(netFlowList);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "NetFlowData", $"var netFlowData = {netFlowDataJson};", true);
            }
        }

        private Dictionary<string, long> CalculateInOutData(List<NetFlowEntity> netFlowList)
        {
            var inOutData = new Dictionary<string, long>
            {
                { "InBytes", 0 },
                { "OutBytes", 0 },
                { "InPkts", 0 },
                { "OutPkts", 0 }
            };

            foreach (var item in netFlowList)
            {
                inOutData["InBytes"] += item.InBytes;
                inOutData["OutBytes"] += item.OutBytes;
                inOutData["InPkts"] += item.InPkts;
                inOutData["OutPkts"] += item.OutPkts;
            }

            return inOutData;
        }
    }
}